import { Component, Input, EventEmitter, TemplateRef, ChangeDetectionStrategy, ViewEncapsulation } from '@angular/core';
import { Router } from '@angular/router';
import { LvConfig } from '../core/config';
import { MenuItem } from '../core/types';
import { routerNavigate, TypeUtils } from '../core/utils/api';

@Component({
  selector: 'lv-breadcrumb',
  exportAs: 'lvBreadcrumb',
  templateUrl: './breadcrumb.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  encapsulation: ViewEncapsulation.None,
  host: {
    '[class.lv-breadcrumb]': 'true',
  },
})
export class BreadcrumbComponent {
  @Input() lvItems: MenuItem[];

  @Input() lvSeparator: string | TemplateRef<void>;

  // LIVE3.0规范 全局配置，路径展示模式，包含完整路径，前缀路径，默认完整路径
  @Input() lvFullPath: boolean = LvConfig.breadcrumbOptions.lvFullPath;

  public typeUtils = TypeUtils;

  constructor(public router: Router) {}

  itemClick(event, item): void {
    if (item.disabled) {
      event.preventDefault();
      return;
    }

    if (item.routerLink) {
      routerNavigate(this.router, item.routerLink);
    }

    if (item.onClick) {
      if (!item._onClickEmitter) {
        item._onClickEmitter = new EventEmitter();
        item._onClickEmitter.subscribe(item.onClick);
      }

      item._onClickEmitter.emit({
        event,
        data: item,
      });
    }
  }
}
